import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';

/// Trade卡片类型
enum GTTradeCardType {
  small,
  large,
}

/// @Title   Trade Card类型的tab
/// @Author: bryce
/// @CreateDate: 2023年12月04日16:13:37
class GTTradeCardTabBar extends StatelessWidget {
  final List<String> tabTitles;
  final TabController? controller;

  // 是否占据父组件总宽度(默认为true)，若为true时，tab平分总宽度；
  final bool isMatchParentWidth;

  final Color selectedLabelColor;

  /// 卡片类型：small:高度24，large:高度40
  final GTTradeCardType? cardType;

  double? height, textSize, radius;

  // 卡片宽度,若传了cardWidth则卡片为固定宽度，否则卡片宽度为自适应文字宽度
  final double? cardWidth;

  // 新增tab切换拦截，如果返回true代表拦截，false允许切换
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptSwitch;

  // 新增拦截点击tab时的切换（对于使用controller控制的和手势滚动TabBarView的切换不拦截）
  final Future<bool> Function(TabController mController, int newIndex)? onInterceptTapSwitch;

  GTTradeCardTabBar(
      {super.key,
      required this.tabTitles,
      required this.selectedLabelColor,
      this.controller,
      this.cardWidth,
      this.isMatchParentWidth = true,
      this.height,
      this.cardType,
      this.onInterceptSwitch,
      this.onInterceptTapSwitch});

  @override
  Widget build(BuildContext context) {
    height ??= cardType == GTTradeCardType.small ? 24 : 40;
    textSize ??= cardType == GTTradeCardType.small ? 12 : 16;
    return Container(
      height: height,
      alignment: isMatchParentWidth ? Alignment.center : null,
      decoration:
          BoxDecoration(color: GTColorsV3.uikitNeutral3V3.color, borderRadius: BorderRadius.circular(radius ?? 3)),
      child: GTTabBar(
          dividerColor: Colors.transparent,
          controller: controller,
          isScrollable: isMatchParentWidth ? false : true,
          unselectedLabelColor: GTColorsV3.text3.color,
          labelColor: GTColorsV3.text6.color,
          labelStyle: gtTextStyleW600(fontSize: textSize),
          unselectedLabelStyle: gtTextStyleW600(fontSize: textSize),
          indicatorSize: TabBarIndicatorSize.label,
          labelPadding: EdgeInsets.zero,
          onInterceptSwitch: onInterceptSwitch,
          onInterceptTapSwitch: onInterceptTapSwitch,
          indicator: BoxDecoration(color: selectedLabelColor, borderRadius: BorderRadius.circular(radius ?? 3)),
          tabs: tabs(height!)),
    );
  }

  List<Widget> tabs(double height) {
    return tabTitles.map((e) => _tabItem(e, height)).toList();
  }

  Widget _tabItem(String title, double height) {
    return Tab(
      child: Container(
        padding: isMatchParentWidth || cardWidth != null ? null : const EdgeInsets.symmetric(horizontal: 16),
        height: height,
        width: cardWidth,
        alignment: Alignment.center,
        child: Text(title),
      ),
    );
  }
}
